package demo.util;


import com.lark.oapi.core.Config;
import com.lark.oapi.core.enums.AppType;
import com.lark.oapi.core.enums.BaseUrlEnum;
import com.lark.oapi.service.im.ImService;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.junit.Test;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LarkUtil {


    // 飞书机器人webhook地址
    private static final String WEBHOOK_URL = "https://open.larksuite.com/open-apis/bot/v2/hook/1c56bbd0-c7c2-4639-a385-82d00d8800e3";

    @Test
    public void testSendText(){
        HttpResponse<String> response = Unirest.post(WEBHOOK_URL)
                .header("Content-Type", "application/json")
                .body("{\n    \"msg_type\": \"text\",\n    \"content\": {\n        \"text\": \"<at user_id=\\\"b582dc1a\\\">Stan.H-OP</at> 报警提醒\"\n    }\n}")
                .asString();
        System.out.println(response);
    }


    public static void main(String[] args) throws Exception {
        // 配置
        Config config = new Config();
        config.setAppType(AppType.SELF_BUILT);
        config.setDomain(BaseUrlEnum.LarkSuite);

        // 创建服务
        ImService imService = new ImService(config);

//        // 构建请求
//        MessageCreateReqBody body = new MessageCreateReqBody();
//        body.setMsgType("text");
//        body.setContent("{\"text\":\"Hello from Java SDK\"}");
//
//        MessageCreateReq req = MessageCreateReq.newBuilder()
//                .receiveIdType("chat_id")  // 或 "open_id", "user_id", "email"
//                .receiveId("oc_123456789") // 群聊ID或用户ID
//                .messageCreateReqBody(body)
//                .build();
//
//        // 发送请求
//        Response<MessageCreateResp> resp = imService.messages.create(req);
//
//        if (resp.getCode() == 0) {
//            System.out.println("消息发送成功，消息ID: " + resp.getData().getMessageId());
//        } else {
//            System.out.println("消息发送失败: " + resp.getMsg());
//        }
    }

    public static void main2(String[] args) {
        // 配置SDK（webhook方式不需要appId和appSecret）
        Config config = new Config();


        // 创建IM服务实例
        ImService imService = new ImService(config);

        // 构建文本消息内容
        String textContent = "{\"text\":\"Hello from Lark Bot Webhook!\"}";

        // 创建请求体
        RequestBody requestBody = RequestBody.create(
                textContent,
                MediaType.parse("application/json; charset=utf-8")
        );
//
//        // 构建发送请求
//        SendMessageReq req = SendMessageReq.newBuilder()
//                .httpMethod("POST")
//                .url(WEBHOOK_URL)
//                .body(requestBody)
//                .build();
//
//        // 发送消息
//        Response<SendMessageResp> resp = imService.messages().send(req);
//
//        // 处理响应
//        if (resp.isSuccess()) {
//            System.out.println("消息发送成功，响应: " + resp.getData());
//        } else {
//            System.err.println("消息发送失败，错误码: " + resp.getCode() + ", 错误信息: " + resp.getMsg());
//        }
    }
}
